package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * & ProjectName：  tensquare-parent
 * & PackageName:   com.tensquare.base.controller
 * & Creation_Date： 2020/3/30 06:08
 * & Author：しの
 * ----------------------------------
 * & Description:
 */
@RestController
@RequestMapping("/label")
@CrossOrigin
public class LabelController {
    @Autowired
    private LabelService labelService;

    //1 查询所有的标签
    @GetMapping
    public Result findAll() {
        //1 服务层查询
        List<Label> labels = labelService.findAll();
        //2 返回结果
        return new Result(true, StatusCode.OK, "查询所有标签成功", labels);
    }

    //2 根据id查询标签
    @GetMapping("{labelId}")
    public Result findLabelById(@PathVariable String labelId) {
        //1 服务层查询
        Label label = labelService.findLabelById(labelId);
        //2 返回
        return new Result(true, StatusCode.OK, "根据id查询标签成功", label);
    }

    //3 添加标签
    @PostMapping
    public Result add(@RequestBody Label label) {
        try {
            //1 服务层添加
            labelService.add(label);
            return new Result(true, StatusCode.OK, "添加标签成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加标签失败");
        }
    }

    //4 修改标签
    @PutMapping("{labelId}")
    public Result update(@PathVariable String labelId, @RequestBody Label label) {
        try {
            //0 绑定id
            label.setId(labelId);
            //1 服务层修改
            labelService.update(label);
            return new Result(true, StatusCode.OK, "修改标签成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "修改标签失败");
        }
    }

    //5 删除标签
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId) {
        try {
            //1 服务层删除
            labelService.delete(labelId);
            return new Result(true, StatusCode.OK, "删除标签成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除标签失败");
        }
    }

    //6 条件查询
    @PostMapping("/search")
    public Result search(@RequestBody Map searchMap) {
        try {
            //1 服务层条件查询
            List<Label> labelList = labelService.search(searchMap);
            //2 返回
            return new Result(true, StatusCode.OK, "条件查询成功", labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "条件查询失败");
        }
    }
    //7 条件查询+分页
    @PostMapping("/search/{page}/{size}")
    public Result searchByPage(@RequestBody Map searchMap,@PathVariable int page,@PathVariable int size){
        return new Result(true,StatusCode.OK,"条件查询+分页成功",labelService.searchByPage(searchMap,page,size));
    }
}
